package com.karry.yl.web.baseinfo;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.baseinfo.Bed;
import com.karry.yl.domain.baseinfo.Building;
import com.karry.yl.domain.baseinfo.Room;
import com.karry.yl.service.baseinfo.BedService;
import com.karry.yl.service.baseinfo.BuildingService;
import com.karry.yl.service.baseinfo.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 楼宇信息Controller
 */
@Controller
@RequestMapping("/building")
public class BuildingController extends SuperController {
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private BedService bedService;
    /**
     * 到楼宇列表页
     * @return
     */
    @RequestMapping
    public String listUI(){
        return "baseinfo/building/list";
    }

    @RequestMapping("/add")
    public String addUI(){
        return "baseinfo/building/add";
    }

    @RequestMapping("edit")
    public String editUI(Long id, ModelMap map){
        Building building = buildingService.selectById(id);
        map.put("building",building);
        return "baseinfo/building/edit";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit){
        Page<Building> buildingPage = buildingService.selectPage(new Page<Building>(page, limit),new EntityWrapper<Building>().eq("orgId",currentUser.getOrgId()).eq("deleted",false).orderBy("createdTime",true));
        return new LayTableResponse(buildingPage.getRecords(),buildingPage.getTotal());
    }

    @RequestMapping("/save")
    public String save(Building building){
        building.setOrgId(currentUser.getOrgId());
        building.setStatus(1);
        buildingService.insert(building);
        return "redirect:/building";
    }

    @RequestMapping("/update")
    public String update(Building building){
        building.setOrgId(currentUser.getOrgId());
        building.setStatus(1);
        buildingService.updateById(building);
        return "redirect:/building";
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids){
        buildingService.deleteBatchIds(ids);
        roomService.delete(new EntityWrapper<Room>().eq("orgId", currentUser.getOrgId()).in("buildingId",ids));
        bedService.delete(new EntityWrapper<Bed>().eq("orgId", currentUser.getOrgId()).in("buildingId",ids));
        return "redirect:/building";
    }
    @RequestMapping("selectBuid")
    @ResponseBody
    public JsonResponse selectBuid(Long buildingId){
        Building building = buildingService.selectById(buildingId);
        return JsonResponse.success(building);
    }
}
